try:
    import json
except ImportError:
    from simplejson import json

class CheckPlugin(object):
    topic = ''

    def __init__(self):
        self.results = []

    def to_str(self, output=True):
        '''output check result'''
        _rs = '='.join([(k, v) for k, v in self.results.items()])
        _ret = '\n'.join([self.topic, _rs])
        if output:
            print _ret
        return _ret

    def to_json(self):
        '''convert stdout to json format'''
        return json.dumps([self.topic, self.results])

    def check(self):
        '''run check'''

class PluginRunTimeException(Exception):
    def __init__(self, retcode, cmd, output=None):
        """Exception raised in which the error occurred

        Attributes:
          cmd -- command line
          stderr -- error message
          retcode -- command exit code
        """
        self.cmd = cmd
        self.retcode = retcode
        self.output = output

    def __str__(self):
        msg = "Command '%s' returned non-zero exit status: %s" % (self.cmd, self.retcode)
        return '\n\n'.join([msg, self.output and self.output or ''])
